#!/usr/bin/python2.5
"""
    Googbus - Engine with DBUS interface for accessing Google APIs
    
    Copyright (C) 2008  Matti Raty

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/.
"""

import os.path
import os

class GoogleUser:
    """
    GoogleUser is class for holding user information and
    preferences.
    
    User file is located in '~/.googbus/googbus.user' file.
    """
    USER_DATA_DIR = "/.googbus/"
    USER_DATA_FILE = "googbus.user"
    USER_DATA_PATH = os.path.expanduser("~") + USER_DATA_DIR + USER_DATA_FILE
    LOG_PREFIX = "GoogleUser: "

    def __init__(self):
        self.username = None
        self.password = None
        
    def read_user_data(self):
        """
        Opens user data file and reads content.
        
        Returns True if data was read successfully, otherwise False.
        """
        ret = False
       
        try:
           file = open(GoogleUser.USER_DATA_PATH, "r")           
        except IOError:
           print "User file " + GoogleUser.USER_DATA_PATH + " not found!"
        else:
           #TODO: Make some real structure for user data file
           self.username = file.readline()
           self.password = file.readline()
           
           ret = True
           
        return ret
    
   
    def write_user_data(self, username, password):
        """
        Writes Googbus user account data to file
        """
        ret = False
        
        try:
            try:
                os.mkdir(os.path.expanduser("~") + GoogleUser.USER_DATA_DIR)
            except:
                print "Datadir already exists, just ignoring.."
                 
            file = open(GoogleUser.USER_DATA_PATH, "w")
                       
        except IOError:
           print "Error while opening " + GoogleUser.USER_DATA_PATH + " for writing!"
        else:
           #TODO: Make some real structure for user data file
           file.write(username+"\n"+password+"\n")
           ret = True
        
        return ret
       
        